package com.sca.auth.controller;

import com.google.common.collect.Lists;
import com.sca.auth.dto.response.LoginResponse;
import com.sca.auth.dto.response.UserDTO;
import com.sca.auth.entity.User;
import com.sca.auth.entity.api.BaseResult;
import com.sca.auth.service.UserService;
import com.sca.auth.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.sca.auth.entity.enums.ResultEnum.ERR_TOKEN_ERROR;
import static com.sca.auth.entity.enums.ResultEnum.ERR_USERNAME_PASSWORD;

/**
 * @author liuxianfei
 * @since 2020/10/30 10:53
 */
@Api
@RestController
@RequestMapping("/user/v1")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/init")
    @ApiOperation("初始化用户")
    public BaseResult<?> initUser() {
        User user = new User();
        user.setUsername("zhangsan");
        user.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        User user2 = new User();
        user2.setUsername("lisi");
        user2.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        List<User> list = Lists.newArrayList(user, user2);
        return new BaseResult<>(userService.saveBatch(list));
    }

    @GetMapping("/{userId}")
    @ApiOperation("获取用户信息")
    @ApiImplicitParam(name = "userId", value = "用户id", paramType = "path", required = true)
    public BaseResult<UserDTO> userInfo(@PathVariable Integer userId) {
        User user = userService.getById(userId);
        if (user != null) {
            return new BaseResult<>(new UserDTO(user.getId(), user.getVersion(), user.getUsername(), user.getUpdateTime()));
        }
        return new BaseResult<>();
    }

    @PostMapping("/updatePw")
    @ApiOperation("修改密码")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户id", paramType = "query", required = true),
        @ApiImplicitParam(name = "password", value = "密码", paramType = "query", required = true)
    })
    public BaseResult<?> updatePassword(@RequestParam Integer userId, @RequestParam String password) {
        return new BaseResult<>(userService.updatePassword(userId, password));
    }

    @PostMapping("/login")
    @ApiOperation("登录")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "username", value = "用户名", paramType = "query", required = true),
        @ApiImplicitParam(name = "password", value = "密码", paramType = "query", required = true)
    })
    public BaseResult<LoginResponse> login(@RequestParam String username, @RequestParam String password) {
        LoginResponse login = userService.login(username, password);
        if (login != null) {
            return new BaseResult<>(login);
        }
        return new BaseResult<>(ERR_USERNAME_PASSWORD);
    }

    @GetMapping("/auth")
    @ApiOperation("认证")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "authorization", value = "token", paramType = "header")
    })
    public BaseResult<?> auth(HttpServletRequest request, HttpServletResponse response) {
        if (JwtUtil.validateToken(request, response)) {
            return new BaseResult<>();
        }
        return new BaseResult<>(ERR_TOKEN_ERROR);
    }


}
